// memberVip/pages/memberPosters/memberPosters.js
var common = require("../../../common/common.js").common;

var app = getApp();

var wxml2canvas = require("../../../common/wxml2Canvas.js");

Page({
    /**
   * 页面的初始数据
   */
    data: {
        zhuanarr: [],
        //会员专享商品
        heights: "",
        heights1: "",
        info: [],
        uids: "",
        //用户id
        er: [],
        urls: "",
        screenWidth: "",
        screenHeight: "",
        domain: "",
        opacity: 0,
        urls: ""
    },
    //获取去右侧胶囊的信息
    getHeader() {
        let menuRect = wx.getMenuButtonBoundingClientRect();
        this.setData({
            topBarHeight: menuRect.height,
            topBarTop: menuRect.top
        });
    },
    //获取用户信息
    getUsersInfomation: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "My/myinfo/myinfo",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                that.setData({
                    info: res.extra,
                    uids: res.extra.m_id
                }, function() {
                    // 二维码
                    common.request({
                        endpoint: "vipcard/vip-poster",
                        method: "POST",
                        data: {
                            uid: this.data.uids,
                            page: "memberVip/pages/memberAuthor/memberAuthor"
                        },
                        success: res => {
                            that.setData({
                                er: res.extra
                            }, function() {
                                setTimeout(() => {
                                    this.drawImage();
                                }, 3e3);
                            });
                        }
                    });
                });
            }
        });
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.getHeader();
        let that = this;
        wx.showLoading({
            title: "海报生成中..."
        });
        that.getUsersInfomation();
        that.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        //首次直接进入，拿openid
                if (app.globalData.user) {
            //如果直接拿到openid
            that.setData({
                openid: app.globalData.user.openid
            });
        } else {
            //回调拿到openid
            app.userInfoReadyCallback = (res => {});
        }
        // 图片自定义宽高
                var query = wx.createSelectorQuery();
        query.select(".cont_bot_left").boundingClientRect();
        query.selectViewport().scrollOffset();
        query.exec(function(res) {
            console.log(res);
            that.setData({
                heights1: res[0].width
            });
        });
        // 专享价商品
                common.request({
            endpoint: "vipcard/vip-product",
            method: "POST",
            data: {
                page: 1
            },
            success: res => {
                that.setData({
                    zhuanarr: res.extra
                });
            }
        });
        //获取用户设备信息，屏幕宽度
                wx.getSystemInfo({
            success: res => {
                that.setData({
                    screenWidth: res.screenWidth,
                    screenHeight: res.screenHeight
                });
            }
        });
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {
        return {
            title: "送你一张VIP卡，赶紧围观",
            path: "memberVip/pages/memberAuthor/memberAuthor?ex=4&count=" + this.data.er.count + "&" + "l=" + this.data.uids,
            // path: 'memberVip/pages/memberAuthor/memberAuthor?experience=4',
            //表示用户点击分享页跳转到的位置
            imageUrl: this.data.domain + "img/fenxiang.jpg",
            //自定义图片的地址
            success(e) {
                wx.showShareMenu({
                    complete: res => {
                        console.log(res);
                    }
                });
            }
        };
    },
    drawImage() {
        let _this = this;
        let systemInfo = wx.getSystemInfoSync();
        let widthss = "";
        let heightss = "";
        if (systemInfo.screenWidth < 375) {
            widthss = 375;
            heightss = 667;
        } else {
            widthss = systemInfo.screenWidth;
            heightss = systemInfo.screenHeight;
        }
        var drawimg = new wxml2canvas({
            width: _this.data.screenWidth,
            height: _this.data.screenHeight,
            element: "canvas1",
            background: "#fff",
            progress(percent) {},
            finish(url) {
                wx.hideLoading();
                setTimeout(function() {
                    wx.canvasToTempFilePath({
                        x: 0,
                        y: 0,
                        canvasId: "canvas1",
                        success: function(res) {
                            _this.setData({
                                urls: res.tempFilePath
                            });
                        }
                    });
                }, 2e3);
            },
            error(res) {}
        });
        let data = {
            list: [ {
                //绘制矩形
                type: "rect",
                x: 0,
                y: 0,
                style: {
                    width: widthss,
                    height: heightss,
                    fill: "#C8A974"
                }
            }, {
                //根据wxml绘制,
                type: "wxml",
                class: ".share__canvas1 .draw_canvas",
                limit: ".share__canvas1",
                x: 0,
                y: 0
            } ]
        };
        drawimg.draw(data);
    },
    // 长按保存图片
    saveImage(e) {
        let _this = this;
        wx.showActionSheet({
            itemList: [ "保存到相册" ],
            success(res) {
                let url = _this.data.urls;
                wx.getSetting({
                    success: res => {
                        if (!res.authSetting["scope.writePhotosAlbum"]) {
                            wx.authorize({
                                scope: "scope.writePhotosAlbum",
                                success: () => {
                                    // 同意授权
                                    _this.saveImgInner(url);
                                },
                                fail: res => {
                                    console.log(res);
                                    wx.showModal({
                                        title: "保存失败",
                                        content: "请开启访问手机相册权限",
                                        success(res) {
                                            wx.openSetting();
                                        }
                                    });
                                }
                            });
                        } else {
                            // 已经授权了
                            _this.saveImgInner(url);
                        }
                    },
                    fail: res => {
                        console.log(res);
                    }
                });
            },
            fail(res) {
                console.log(res.errMsg);
            }
        });
    },
    // 长按保存功能--保存部分
    saveImgInner(url) {
        wx.getImageInfo({
            src: url,
            success: res => {
                let path = res.path;
                wx.saveImageToPhotosAlbum({
                    filePath: path,
                    success: res => {
                        console.log(res);
                        wx.showToast({
                            title: "已保存到相册"
                        });
                    },
                    fail: res => {
                        console.log(res);
                    }
                });
            },
            fail: res => {
                console.log(res);
            }
        });
    }
});